package com.bookmanage.dao;

import com.bookmanage.book.Book;
import com.bookmanage.utils.DBUitls;

import java.util.ArrayList;

import java.sql.*;

public class BookConn {
    //用于存Book
    static ArrayList<Book> list = new ArrayList<Book>();

    public static ArrayList<Book> list_return() {

        try {

            Connection conn = DBUitls.getConn();

            String sql = "select * from book ";
            //预处理编译sql
            PreparedStatement pst = (PreparedStatement) DBUitls.getConn().prepareStatement(sql);
            ResultSet rs = (ResultSet) pst.executeQuery();
            if (conn != null) {
                // System.out.print("success!");
                while (rs.next()) {
                    //从数据库获取书籍信息
                    int isbn = rs.getInt("isbn");
                    int price = rs.getInt("price");
                    String publish = rs.getString("publish");
                    String author = rs.getString("author");
                    String bookname = rs.getString("bookname");
                    //存入到Book类里面
                    Book book = new Book();
                    book.setIsbn(isbn);
                    book.setPrice(price);
                    book.setAuthor(author);
                    book.setBookname(bookname);
                    book.setPublish(publish);
                    list.add(book);
                }

            } else {
                System.out.println("数据库连接失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //将list返回，方便jsp界面调用
        return list;
    }
}
